(function (vc) {
    vc.extends({
        data: {
            initDataInfo: {
                loginUsers: [],
                pageUrl: ''
            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('initData', 'loadCommunityInfo', function (_param) {
                $that.initDataInfo.loginUsers = _param.loginUsers;
                $that.initDataInfo.pageUrl = _param.url;
                if ($that.initDataInfo.loginUsers.length > 1) {
                    $that._initOpenModal();
                    return;
                }
                $that._initLoginUserInfo($that.initDataInfo.loginUsers[0]);
            })
        },
        methods: {
            _initOpenModal: function () {
                var content = '<div style="padding: 20px;">';
                content += '<table class="layui-table">';
                content += '<thead><tr><th>选择</th><th>账号</th><th>隶属</th><th>角色</th></tr></thead>';
                content += '<tbody>';
                $that.initDataInfo.loginUsers.forEach(function (item) {
                    content += `<tr>
                    <td><input type="radio" name="selectedItem" value='${JSON.stringify(item)}'></td>
                    <td>${item.userName}(${item.userId})</td>
                    <td>${item.storeName}</td>
                    <td>${item.relCdName}</td>
                  </tr>`;
                });
                content += '</tbody></table>';
                content += '</div>';

                // 打开弹框
                layer.open({
                    type: 1,
                    title: '请选择账号',
                    content: content,
                    area: ['600px', '400px'],
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        // 获取用户选择的数据
                        let selected = document.querySelector('input[name="selectedItem"]:checked');
                        if (selected) {
                            let selectedData = JSON.parse(selected.value);
                            $that._initLoginUserInfo(selectedData);

                        } else {
                            vc.toast('请选择');
                        }
                        layer.close(index); // 关闭弹框
                    },
                    btn2: function (index, layero) {
                        layer.close(index); // 关闭弹框
                    }
                });
            },
            _initLoginUserInfo: function (_user) {
                vc.saveData('hc_accessToken', {
                    token: _user.token
                });
                $that._validateHasStore();
            },
            _loadCommunityInfo: function () {
                let param = {
                    params: {
                        _uId: 'ccdd00opikookjuhyyttvhnnjuuu',
                        page: 1,
                        row: 3
                    }
                };
                vc.http.apiGet('/community.listMyEnteredCommunitys',
                    param,
                    function (json, res) {
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            let _communityInfos = _json.communitys;
                            if (_communityInfos && _communityInfos.length > 0) {
                                vc.setCurrentCommunity(_communityInfos[0]);
                                vc.setCommunitys(_communityInfos);
                            } else {
                                vc.toast('运营团队未分配小区，请联系运营团队');
                                return;
                            }
                            vc.jumpToPage($that.initDataInfo.pageUrl);
                        }
                    },
                    function () {
                        vc.jumpToPage($that.initDataInfo.pageUrl);
                    }
                );
            },
            _validateHasStore: function () {
                let param = {
                    params: {
                        _uId: 'ccdd00opikookjuhyyttvhnnjuuu'
                    }
                };
                vc.http.apiGet('/query.store.byuser',
                    param,
                    function (json, res) {
                        if (res.status == 200) {
                            $that._loadStaffPrivileges();
                        } else {
                            vc.toast(json);
                        }
                    },
                    function (e, res) {
                        console.log('请求失败处理', e);
                        //vc.jumpToPage(_param.url);
                        vc.toast(e);
                    }
                );
            },
            _loadStaffPrivileges: function (_param) {

                let param = {
                    params: {
                        a: 'HC'
                    }
                };
                //发送get请求
                vc.http.apiGet('/query.user.privilege',
                    param,
                    function (json) {
                        let _json = JSON.parse(json);

                        let _privilege = [];
                        _json.datas.forEach(item => {
                            _privilege.push(item.pId);
                        });

                        vc.saveData('hc_staff_privilege', _privilege);
                        $that._loadCommunityInfo();
                    },
                    function () {
                        console.log('请求失败处理');
                    });
            },
        }
    });


})(window.vc);